import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.Map;

public class Config {
    private Map<String, Object> config;

    public Config() {
        try {
            File file = new File(System.getProperty("user.dir") + "\\config.yml");
            if (!file.exists()) {
                file.createNewFile();
                InputStream in = Main.class.getClassLoader().getResourceAsStream("config.yml");
                FileOutputStream out = new FileOutputStream(file);
                int read;
                byte[] buffer = new byte[1024];
                while ((read = in.read(buffer)) != -1)
                    out.write(buffer, 0, read);
                in.close();
                out.flush();
                out.close();
            }
            InputStream in = new FileInputStream(file);
            config = new Yaml().loadAs(in, Map.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Config(Map<String, Object> config) {
        this.config = config;
    }

    public Config(Object config) {
        this.config = (Map<String, Object>) config;
    }

    public Map<String, Object> getConfig() {
        return config;
    }

    public void setConfig(Map<String, Object> config) {
        this.config = config;
    }

    public Object get(String key) {
        if (key.contains("."))
            return new Config(config.get(key.split("\\.")[0])).get(key.substring(key.indexOf(".") + 1));
        return config.get(key);
    }

    public String getString(String key) {
        if (key.contains("."))
            return String.valueOf(new Config(config.get(key.split("\\.")[0])).get(key.substring(key.indexOf(".") + 1)));
        return String.valueOf(config.get(key));
    }

    public Integer getInteger(String key) {
        if (key.contains("."))
            return Integer.valueOf(String.valueOf(new Config(config.get(key.split("\\.")[0])).get(key.substring(key.indexOf(".") + 1))));
        return Integer.valueOf(String.valueOf(config.get(key)));
    }

    public Long getLong(String key) {
        if (key.contains("."))
            return Long.valueOf(String.valueOf(new Config(config.get(key.split("\\.")[0])).get(key.substring(key.indexOf(".") + 1))));
        return Long.valueOf(String.valueOf(config.get(key)));
    }

    public Double getDouble(String key) {
        if (key.contains("."))
            return Double.valueOf(String.valueOf(new Config(config.get(key.split("\\.")[0])).get(key.substring(key.indexOf(".") + 1))));
        return Double.valueOf(String.valueOf(config.get(key)));
    }

    public Boolean getBoolean(String key) {
        if (key.contains("."))
            return Boolean.valueOf(String.valueOf(new Config(config.get(key.split("\\.")[0])).get(key.substring(key.indexOf(".") + 1))));
        if (String.valueOf(config.get(key)).equals("on"))
            return true;
        if (String.valueOf(config.get(key)).equals("off"))
            return false;
        return null;
    }
}
